package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.util.ChZhRJUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

public abstract class DiscoverViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(listener != null) {
            listener.onClickHideDialog();
        }
        return false;
    }

    public interface OnClickFriendListener {

        void onClickLike(DiscoverViewHolder holder);

        void onClickComment(DiscoverViewHolder holder);

        void onClickCommentPosition(DiscoverViewHolder holder, int position);

        void onClickShowDialog(DiscoverViewHolder holder);

        void onClickHideDialog();

    }

    private FrameLayout contentLayout;

    private TextView textUserName;

    private RoundedImageView imageUserHead;

    private TextView textBabyName;

    private CuteBabyAvatarView imageBabyHead;

    private TextView textTime;

    private TextView textLikeNumber;

    private TextView textCommentNumber;

    private RelativeLayout layoutBottom;

    private ImageView imageLike;

    private ImageView imageLikeList;

    private TextView textLikeList;

    private boolean showLike = false, showComment = false;

    private LinearLayout layoutComment;

    private OnClickFriendListener listener;

    private ArrayList<Integer> ids;

    private ImageButton imageShowComment;
    private CuteTextView btnLike, btnComment;
    private View commentDialog;

    public DiscoverViewHolder(View itemView) {
        super(itemView);
        contentLayout = (FrameLayout) itemView.findViewById(R.id.content);
        textUserName = (TextView) itemView.findViewById(R.id.text_user_name);
        imageUserHead = (RoundedImageView) itemView.findViewById(R.id.image_user_head);
        textBabyName = (TextView) itemView.findViewById(R.id.text_baby_name);
        imageBabyHead = (CuteBabyAvatarView) itemView.findViewById(R.id.image_baby_head);
        textTime = (TextView) itemView.findViewById(R.id.text_time);
        imageLike = (ImageView) itemView.findViewById(R.id.image_like);
        imageLike.setOnClickListener(this);
        ImageView imageComment = (ImageView) itemView.findViewById(R.id.image_comment);
        imageComment.setOnClickListener(this);
        textLikeNumber = (TextView) itemView.findViewById(R.id.text_like_number);
        textCommentNumber = (TextView) itemView.findViewById(R.id.text_comment_number);

        layoutBottom = (RelativeLayout) itemView.findViewById(R.id.layout_bottom);
        imageLikeList = (ImageView) itemView.findViewById(R.id.image_like_left);
        textLikeList = (TextView) itemView.findViewById(R.id.text_like);
        layoutComment = (LinearLayout) itemView.findViewById(R.id.layout_comment);

        // 点击弹出评论
        itemView.setOnTouchListener(this);
        commentDialog = itemView.findViewById(R.id.comment_dialog);
        imageShowComment = (ImageButton) itemView.findViewById(R.id.image_show_comment);
        imageShowComment.setOnClickListener(this);
        // 赞
        btnLike = (CuteTextView) itemView.findViewById(R.id.btn_like);
        btnLike.setOnClickListener(this);
        // 评论
        btnComment = (CuteTextView) itemView.findViewById(R.id.btn_comment);
        btnComment.setOnClickListener(this);

        ids = new ArrayList<Integer>();
    }

    public void setOnClickFriendListener(OnClickFriendListener listener) {
        this.listener = listener;
    }

    protected final int getContentWidth() {
        RelativeLayout layout = ((RelativeLayout)getContent().getParent());
        DisplayMetrics displayMetrics = layout.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels - (int) ChZhRJUtils.dp2px(50, displayMetrics) - layout.getPaddingLeft() - layout.getPaddingRight();
    }

    protected final FrameLayout getContent() {
        return contentLayout;
    }

    public void setUserName(String text) {
        textUserName.setText(text);
    }

    public void setImageUserHeadUrl(String url) {
        ImageLoader.getInstance().displayImage(url, imageUserHead, Config.getOptionsHead());
    }

    public void setBabyName(String text) {
        textBabyName.setText(text);
    }

    public void setBabyHeadUrl(String url, int sex) {
        // ImageLoader.getInstance().displayImage(url, imageBabyHead, Config.getOptionsHeadUnKnown());
        imageBabyHead.setAvatar(url);
        imageBabyHead.setSex(sex);
    }

    public void setTime(String text) {
        textTime.setText(text);
    }

    public void setIsLike(Context context, boolean isLike) {
        if (!isLike) {
            btnLike.setText("赞");
            ((LinearLayout.LayoutParams)btnLike.getLayoutParams()).weight = 1.5f;
            imageLike.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_friend_like_1));
        } else {
            ((LinearLayout.LayoutParams)btnLike.getLayoutParams()).weight = 2.0f/3;
            btnLike.setText("取消赞");
            imageLike.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_friend_like_2));
        }
    }

    // 隐藏评论弹出框
    public void setHideDialog() {
        commentDialog.setVisibility(View.INVISIBLE);
    }

    public void setLikeNumber(int likeNumber) {
//        if (likeNumber == 0) {
//            textLikeNumber.setVisibility(View.INVISIBLE);
//        } else {
//            textLikeNumber.setVisibility(View.VISIBLE);
//            textLikeNumber.setText("" + likeNumber);
//        }
    }

    public void setCommentNumber(int commentNumber) {
//        if (commentNumber == 0) {
//            textCommentNumber.setVisibility(View.INVISIBLE);
//        } else {
//            textCommentNumber.setVisibility(View.VISIBLE);
//            textCommentNumber.setText("" + commentNumber);
//        }
    }

    public void setBottomVisibility(boolean visibility) {
        if (visibility) {
            layoutBottom.setVisibility(View.VISIBLE);
        } else {
            layoutBottom.setVisibility(View.GONE);
        }
    }

    public void setLikeVisibility(boolean visibility) {
        showLike = visibility;
        if (visibility) {
            imageLikeList.setVisibility(View.VISIBLE);
            textLikeList.setVisibility(View.VISIBLE);
        } else {
            imageLikeList.setVisibility(View.GONE);
            textLikeList.setVisibility(View.GONE);
        }
    }

    public void setLikeList(ArrayList<String> likeList) {
        if (likeList == null) {
            return;
        }
        String content = "";
        for (int i = 0; i < likeList.size(); i++) {
            if (i < likeList.size() - 1) {
                content += likeList.get(i) + ",";
            } else {
                content += likeList.get(i);
            }
        }
        textLikeList.setIncludeFontPadding(false);
        textLikeList.setText(content);
    }

    public void setCommentVisibility(boolean visibility) {
        showComment = visibility;
        if (visibility) {
            layoutComment.setVisibility(View.VISIBLE);
        } else {
            layoutComment.setVisibility(View.GONE);
        }
    }

    public void setCommentList(Context context, ArrayList<DiscoverAdapter.CommentModel> commentList) {
        if (commentList == null) {
            return;
        }
        ids.clear();
        layoutComment.removeAllViews();
        boolean multi;
        // [0]起点1
        // [1]长度1
        // [2]起点2
        // [3]长度2
        int[][] color = new int[commentList.size()][4];
        for (int i = 0; i < commentList.size(); i++) {
            String content = "";
            if (!TextUtils.isEmpty(commentList.get(i).getUserName2())) {
                multi = true;
                color[i][0] = 0;
                content += commentList.get(i).getUserName1() + " 回复 " + commentList.get(i).getUserName2() + ": " + commentList.get(i).getContent();
                color[i][1] = (commentList.get(i).getUserName1() == null ? 4 : commentList.get(i).getUserName1().length());
                color[i][2] = color[i][1] + 4;
                color[i][3] = color[i][2] + (commentList.get(i).getUserName2() == null? 4 : commentList.get(i).getUserName2().length()) + 1;
            } else {
                multi = false;
                color[i][0] = 0;
                content += commentList.get(i).getUserName1() + ": " + commentList.get(i).getContent();
                color[i][1] = (commentList.get(i).getUserName1() == null? 4 : commentList.get(i).getUserName1().length()) + 1;
            }

            ForegroundColorSpan colorSpan = new ForegroundColorSpan(0xff7f3fa2);
            SpannableStringBuilder builder = new SpannableStringBuilder(content);
            if (multi) {
                builder.setSpan(colorSpan, color[i][0], color[i][1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(0xff7f3fa2);
                builder.setSpan(colorSpan2, color[i][2], color[i][3], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                builder.setSpan(colorSpan, color[i][0], color[i][1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            TextView textView = new TextView(context);
            textView.setId(i);
            textView.setTextSize(12);
            textView.setTextColor(Color.BLACK);
            textView.setText(builder);
            textView.setIncludeFontPadding(false);
            float paddingTop = ChZhRJUtils.dp2px(9, itemView.getResources().getDisplayMetrics());
            float paddingLeft = ChZhRJUtils.dp2px(8, itemView.getResources().getDisplayMetrics());
            if(i == 0) {
                // textView.setBackgroundColor(Color.BLACK);
                if(showLike) {
                    textView.setPadding((int)paddingLeft, (int)(paddingTop / 3), (int)paddingLeft, (int)(paddingTop / 2));
                } else {
                    textView.setPadding((int)paddingLeft, (int)(paddingTop * 2 / 3), (int)paddingLeft, (int)(paddingTop / 2));
                }
            }
            else if(i == commentList.size() - 1) {
                textView.setPadding((int)paddingLeft, (int)(paddingTop / 2), (int)paddingLeft, (int)(paddingTop * 2 / 3));
            }
            else {
                textView.setPadding((int)paddingLeft, (int)(paddingTop / 2), (int)paddingLeft, (int)paddingTop / 2);
            }
            textView.setOnClickListener(this);
            layoutComment.addView(textView);
            ids.add(i);
        }
    }

    @Override
    public void onClick(View v) {
        if(listener == null) return;
        if (v.getId() == R.id.image_show_comment) {
            commentDialog.setVisibility(View.VISIBLE);
            listener.onClickShowDialog(this);
        }
        else if(v.getId() == R.id.btn_like) {
            commentDialog.setVisibility(View.INVISIBLE);
            if (listener == null) {
                return;
            }
            listener.onClickLike(this);
        }
        else if(v.getId() == R.id.btn_comment) {
            commentDialog.setVisibility(View.INVISIBLE);
            if (listener == null) {
                return;
            }
            listener.onClickComment(this);
        }
        else {
            for (int i : ids) {
                if (v.getId() == i) {
                    listener.onClickCommentPosition(this, i);
                    return;
                }
            }
        }
    }
}
